import { CordModel, ThingSizeModel } from "./base/model";


export class Map {
    private contentWidthUnits = -1;
    private contentHeightUnits = 15;
    public blockArr: CordModel[] = []; // 不可破坏的砖头
    public softBlockArr: CordModel[] = []; // 可破坏的砖头
    public wallSize = 20;
    // @ts-ignore
    curScene: Phaser.Scene;
    // @ts-ignore
    walls: Phaser.Physics.Arcade.StaticGroup;
    // @ts-ignore
    blocks: Phaser.Physics.Arcade.StaticGroup;

    constructor(private game: Phaser.Game) {
    }

    public createMap() {
        this.curScene = this.game.scene.getAt(0)!;
        this.contentWidthUnits = Math.floor(parseInt(this.game.config.width as string, 10) / this.wallSize);

        this.blockArr = this.initBlockArr();

        // todo: asd
        this.blockArr.push({ x: 1, y: 0 });
        this.blockArr.push({ x: 2, y: 0 });
        this.blockArr.push({ x: 3, y: 0 });
        this.blockArr.push({ x: 4, y: 0 });
        this.walls = this.createWall(this.blockArr);


        this.softBlockArr = this.initSoftBlockArr();
        this.blocks = this.createBlock(this.softBlockArr);
    }

    private initSoftBlockArr(): CordModel[] {
        let arr: CordModel[] = [];
        for (let i = 0; i < this.contentWidthUnits; i++) {
            for (let j = 0; j < this.contentHeightUnits; j++) {
                if (this.blockArr.findIndex(item => item.x === i && item.y === j) < 0) {
                    arr.push({ x: i, y: j });
                }
            }
        }

        return arr;
    }

    private initBlockArr(): CordModel[] {
        let arr: CordModel[] = [];

        // 底边
        for (let i = 0; i < this.contentWidthUnits; i++) {
            if (i > 5 && i < 15) {
                continue;
            }
            if (i > 25 && i < 35) {
                continue;
            }
            if (i > 50 && i < 75) {
                continue;
            }
            arr.push({ x: i, y: 25 });
        }

        // 两侧
        for (let i = 0; i < this.contentHeightUnits; i++) {
            arr.push({ x: 0, y: i });
            arr.push({ x: this.contentWidthUnits - 1, y: i });
        }
        return arr;
    }

    /**
     * 绘制墙面，不可被破坏
     */
    private createWall(arr: CordModel[]): Phaser.Physics.Arcade.StaticGroup {
        let walls = this.curScene.physics.add.staticGroup();
        arr.forEach(item => {
            walls.create(item.x * this.wallSize, item.y * this.wallSize, 'wall');
        });
        return walls;
    }

    /**
     * 绘制砖块，可被破坏
     */
    private createBlock(arr: CordModel[]): Phaser.Physics.Arcade.StaticGroup {
        let blocks = this.curScene.physics.add.staticGroup();
        arr.forEach(item => {
            blocks.create(item.x * this.wallSize, item.y * this.wallSize, 'block');
        });
        // blocks.setVelocity(1, 0);
        // blocks.children.iterate(item => {
        //     // @ts-ignore
        //     item.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
        //     this.curScene.physics.collide(item, this.walls);

        // });
        return blocks;
    }

    public refreshSoftWall() {
        // this.game.stage.removeChild(this.softGraphics);
        // this.softGraphics = this.createWall(this.softBlockArr, true);
    }
}
